import 'package:flutter/material.dart';
import '../../../core/platform/platform_detector.dart';

/// 自适应按钮组件，根据平台调整样式
class AdaptiveButton extends StatelessWidget {
  final VoidCallback? onPressed;
  final Widget child;
  final bool isLoading;
  final bool isPrimary;
  final double? width;
  final double? height;
  final EdgeInsets? padding; // 添加自定义内边距参数

  const AdaptiveButton({
    super.key,
    required this.onPressed,
    required this.child,
    this.isLoading = false,
    this.isPrimary = true,
    this.width,
    this.height,
    this.padding, // 添加自定义内边距参数
  });

  @override
  Widget build(BuildContext context) {
    final buttonHeight = height ?? PlatformDetector.getButtonHeight();

    // 桌面平台使用鼠标悬停效果
    if (PlatformDetector.isDesktop() || PlatformDetector.isWeb()) {
      return MouseRegion(
        cursor: isEnabled ? SystemMouseCursors.click : SystemMouseCursors.basic,
        child: SizedBox(
          width: width,
          height: buttonHeight,
          child: ElevatedButton(
            onPressed: isEnabled ? onPressed : null,
            style: isPrimary
                ? ElevatedButton.styleFrom(
                    backgroundColor: Theme.of(context).primaryColor,
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                    padding: padding ?? EdgeInsets.zero, // 使用自定义内边距或默认值
                    elevation: 2, // 添加轻微阴影
                    shadowColor: Colors.black.withValues(alpha: 0.1), // 阴影颜色
                  )
                : ElevatedButton.styleFrom(
                    backgroundColor: Colors.grey[100], // 更浅的背景色
                    foregroundColor: Colors.black87,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                    padding: padding ?? EdgeInsets.zero, // 使用自定义内边距或默认值
                    side: BorderSide(color: Colors.grey[300]!), // 添加边框
                  ),
            child: isLoading
                ? SizedBox(
                    width: 20,
                    height: 20,
                    child: CircularProgressIndicator(
                      strokeWidth: 2,
                      valueColor: AlwaysStoppedAnimation<Color>(
                        isPrimary ? Colors.white : Colors.black87,
                      ),
                    ),
                  )
                : child,
          ),
        ),
      );
    } else {
      // 移动平台使用默认按钮
      return SizedBox(
        width: width,
        height: buttonHeight,
        child: ElevatedButton(
          onPressed: isEnabled ? onPressed : null,
          style: isPrimary
              ? ElevatedButton.styleFrom(
                  backgroundColor: Theme.of(context).primaryColor,
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8),
                  ),
                  padding: padding ?? EdgeInsets.zero,
                  elevation: 2, // 添加轻微阴影
                  shadowColor: Colors.black.withValues(alpha: 0.1), // 阴影颜色
                )
              : ElevatedButton.styleFrom(
                  backgroundColor: Colors.grey[100], // 更浅的背景色
                  foregroundColor: Colors.black87,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8),
                  ),
                  padding: padding ?? EdgeInsets.zero,
                  side: BorderSide(color: Colors.grey[300]!), // 添加边框
                ),
          child: isLoading
              ? SizedBox(
                  width: 20,
                  height: 20,
                  child: CircularProgressIndicator(
                    strokeWidth: 2,
                    valueColor: AlwaysStoppedAnimation<Color>(
                      isPrimary ? Colors.white : Colors.black87,
                    ),
                  ),
                )
              : child,
        ),
      );
    }
  }

  bool get isEnabled => onPressed != null && !isLoading;
}
